package com.example.SearchAround_JC.jc_activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.SearchAround_JC.R;
import com.example.SearchAround_JC.jc_manager.PoiDetails;
import com.example.SearchAround_JC.jc_manager.PoiResult;
import org.json.JSONException;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
//    AK:         2.00AEMTsFI8k_KEe41fe2200089dCuC
//    请求URL：https://api.weibo.com/2/location/pois/search/by_geo.json
//    请求参数：cenname=%E9%92%9F%E6%A5%BC&access_token=2.00AEMTsFI8k_KEe41fe2200089dCuC
    private Button mainSearchBt;
    private Button mainSettingBt;
    private TextView mainLocationTextView;
    private Button mainLocationBt;
    private ListView mainListView;

    LocationClient mlocationClient;
    private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
    private String tempcoor = "gcj02";

    List<String> list = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);


        mainSearchBt = (Button) findViewById(R.id.main_search_bt);
        mainSearchBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });
        mainLocationTextView = (TextView) findViewById(R.id.main_location_textView);
        SharedPreferences spf = getSharedPreferences("coordinate",MODE_PRIVATE);
        String cityname = spf.getString("city","请确定位自己的位置");
        mainLocationTextView.setText(cityname);
        initData();
        mainListView = (ListView) findViewById(R.id.main_ListView);
        MainListViewAdapter adapter = new MainListViewAdapter(this, R.layout.mainlistview_item, list);
        mainListView.setAdapter(adapter);

        final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("定位中，请稍等....");
        progressDialog.setProgressDrawable(null);

        mlocationClient = new LocationClient(MainActivity.this);
        initLocation();
        mlocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                Log.d("dfafa","fgaf");
                progressDialog.dismiss();

                //或许经纬度
                double y = location.getLatitude();
                double x = location.getLongitude();
                SharedPreferences.Editor sharedPreferences =
                        getSharedPreferences("coordinate",MODE_PRIVATE).edit();
                sharedPreferences.putString("coordinate",x+","+y);
                //获取精确地点
                String add = location.getAddrStr();
                sharedPreferences.putString("city",add);
                sharedPreferences.commit();
                mainLocationTextView.setText("当前位置："+add);
//                String str[] = add.split("市");
//                mainLocationTextView.setText("当前位置："+str[1]);
            }
        });

        mainLocationBt = (Button) findViewById(R.id.main_location_bt);
        mainLocationBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.show();

                mlocationClient.start();
                Log.d("258","fafa");
                mlocationClient.requestLocation();
            }
        });

        mainSettingBt = (Button) findViewById(R.id.main_setting_bt);
        mainSettingBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SettingActivity.class);
                startActivity(intent);
            }
        });

    }

    class MainListViewAdapter extends ArrayAdapter<String> {
        int resourceId;

        public MainListViewAdapter(Context context, int resource, List<String> objects) {
            super(context, resource, objects);
            resourceId = resource;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final String mainStrItem = getItem(position);
            View view = LayoutInflater.from(MainActivity.this).inflate(resourceId, null);
            view.setMinimumHeight(60);
            TextView textView = (TextView) view.findViewById(R.id.mainListViewItem_TextView);
            textView.setText(mainStrItem);
            Button button = (Button) view.findViewById(R.id.mainListViewItem_Button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("button", "main界面点击事件");
                    Intent intent = new Intent(MainActivity.this, Second_Activity.class);
                    intent.putExtra("firstPosition",position);
                    intent.putExtra("firstItemName",mainStrItem);
                    startActivity(intent);
                }
            });
            return view;
        }
    }

    //初始化数据
    private void initData() {
        String str1 = "餐饮服务";
        list.add(str1);

        String str2 = "购物服务";
        list.add(str2);

        String str3 = "生活服务";
        list.add(str3);

        String str4 = "体育休闲服务";
        list.add(str4);

        String str5 = "住宿服务";
        list.add(str5);

        String str6 = "风景名胜";
        list.add(str6);

        String str7 = "医疗保健服务";
        list.add(str7);

        String str8 = "科教文化服务";
        list.add(str8);

        String str9 = "交通设施服务";
        list.add(str9);

        String str10 = "金融保险服务";
        list.add(str10);

        String str11 = "公共设施";
        list.add(str11);
    }

    //初始化定位条件
    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);                        //打开gps
        option.setLocationMode(tempMode);//设置定位模式
        option.setCoorType(tempcoor);//返回的定位结果是百度经纬度，默认值gcj02
        option.setIsNeedAddress(true);
        mlocationClient.setLocOption(option);
    }
}
